﻿(function ($) {
    jQuery.fn.carousel = function () {
        return this.each(function () {
            var $container = jQuery(this);

            // $container.find(".carousel-paging").show();
            // Activate first link
            $container.find(".carousel-paging a:first").addClass("active");

            //Paging and Slider Function
            rotate = function ($container) {
                //Get size of the image, how many images there are, then determin the size of the image reel.
                var imageWidth = $container.find(".carousel-window").width();
                var imageSum = $container.find(".carousel-imageReel img").size();
                var imageReelWidth = imageWidth * imageSum;

                //Adjust the image reel to its new size
                $container.find(".carousel-imageReel").css({ 'width': imageReelWidth });

                var $active = $container.data('active');
                var triggerID = $active.attr("rel") - 1; //Get number of times to slide
                var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

                if (!isNaN(image_reelPosition)) {
                    $container.find(".carousel-paging a").removeClass('active'); //Remove all active class
                    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

                    //Slider Animation
                    $container.find(".carousel-imageReel").animate({
                        left: -image_reelPosition
                    }, 500);
                }
            };

            //Rotation and Timing Event
            rotateSwitch = function ($container) {
                play = setInterval(function () { //Set timer - this will repeat itself every 7 seconds
                    $active = $container.find(".carousel-paging a.active").next(); //Move to the next paging
                    if ($active.length === 0) { //If paging reaches the end...
                        $active = $container.find(".carousel-paging a:first"); //go back to first
                    }
                    $container.data('active', $active);
                    rotate($container); //Trigger the paging and slider function
                }, 7000); //Timer speed in milliseconds (7 seconds)
                $container.data('play', play);
            };

            rotateSwitch($container); //Run function on launch

            //On Hover
            jQuery(".carousel-imageReel img").hover(function () {
                var $container = $(this).parents(".carousel-mainView");
                var play = $container.data('play');
                clearInterval(play); //Stop the rotation

            }, function () {
                var $container = $(this).parents(".carousel-mainView");
                var play = $container.data('play');
                clearInterval(play); //Stop the rotation
                rotateSwitch($container); //Resume rotation
            });
        }); // end each()
    };
})(jQuery);